/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-04
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-06
 * @Description: 聊天列表页
 */
import 'package:flutter/material.dart';
import 'package:chat_client/models/conversation.dart';
import 'package:chat_client/screens/chat/chat_screen.dart';
import 'package:chat_client/widgets/global_widgets.dart'; // 引入公共的组件

class ChatListScreen extends StatefulWidget {
  const ChatListScreen({super.key});

  @override
  State<StatefulWidget> createState() {
    return _ChatListScreenState();
  }
}

// 聊天列表页状态
class _ChatListScreenState extends State<ChatListScreen>
//在切换时不被销毁
    with
        AutomaticKeepAliveClientMixin<ChatListScreen> {
  // 模拟的聊天数据
  final List<Conversation> messages = [];

  @override
  bool get wantKeepAlive => true; // 保持页面状态

  @override
  Widget build(BuildContext context) {
    super.build(context); // 必须调用 super.build 以使 keepAlive 生效
    return ListView.separated(
        // 列表项数
        itemCount: messages.length,
        // 构建每一项
        itemBuilder: (context, index) {
          return customListInfo(
            title: messages[index].sessionObject,
            subtitle: messages[index].lastMessage,
            trailing: Text(messages[index].lastActive as String),
            imageUrl: 'assets/images/avatar16.jpg',
            size: 60,
            onTap: () {
              // 点击进入聊天页面
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      ChatScreen(user: messages[index].sessionObject),
                ),
              );
            },
          );
        },
        // 分隔符
        separatorBuilder: (context, index) => buildDivider());
  }
}
